package com.ktwlsoft.fundmanage.service;

import com.ktwlsoft.fundmanage.domain.entity.GroupDuty;
import com.ktwlsoft.fundmanage.domain.repository.GroupDutyRepository;
import com.ktwlsoft.fundmanage.payload.BaseResult;
import com.ktwlsoft.fundmanage.payload.ResultType;
import com.ktwlsoft.fundmanage.utils.KeyWorkerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 用户任务配置
 *
 * @author Name on 2019/9/5 12:04
 * @version 1.0
 */
@Service
public class UserTaskConfigService {

    /**角色或组职责*/
    private final GroupDutyRepository groupDutyRepository;

    public UserTaskConfigService(GroupDutyRepository groupDutyRepository){
        this.groupDutyRepository = groupDutyRepository;
    }

    /**
      * 查询数据库中的所有数据 on 2019/9/5 17:03
      *
      * @return BaseResult<?>
      */
    public BaseResult<?> findAll(){
        return BaseResult.body(groupDutyRepository.findAll(),ResultType.BUSINESS_SUCCESS);
    }

    /**
      * 修改是否启用 on 2019/9/5 15:31
      *
      * @param id id
      * @return BaseResult<?>
      */
    public BaseResult<?> updateActive(Long id){
        GroupDuty groupDuty = groupDutyRepository.findById(id).orElse(null);
        if(groupDuty == null){
            return BaseResult.body(id,ResultType.BUSINESS_ERROR).withMessage("该数据不存在");
        }
        if(groupDuty.isAcitive()){
            groupDuty.setAcitive(false);
        }else{
            groupDuty.setAcitive(true);
        }
        try {
            groupDutyRepository.save(groupDuty);
        }catch (Exception e){
            e.printStackTrace();
            return BaseResult.body(id,ResultType.BUSINESS_ERROR);
        }
        return BaseResult.body(id,ResultType.BUSINESS_SUCCESS);
    }

    /**
      * 删除 on 2019/9/5 15:23
      *
      * @param groupDuty groupDuty
      * @return BaseResult<?>
      */
    public BaseResult<?> delete(GroupDuty groupDuty){
        try {
            groupDutyRepository.delete(groupDuty);
        }catch (Exception e){
            e.printStackTrace();
            return BaseResult.body(groupDuty, ResultType.BUSINESS_ERROR);
        }
        return BaseResult.body(groupDuty, ResultType.BUSINESS_SUCCESS);
    }

    /**
      * 新增或修改 on 2019/9/5 15:15
      *
      * @param groupDuty groupDuty
      * @return BaseResult<?>
      */
    public BaseResult<?> save(GroupDuty groupDuty){
        //限制为修改
        if(groupDuty.getId() == null || groupDuty.getId() <= 0){
            return BaseResult.body(groupDuty,ResultType.BUSINESS_ERROR).withMessage("id为空或不正确");
        }
        if(groupDuty.getRoleName() == null || "".equals(groupDuty.getRoleName())){
           if(groupDuty.getGroupName() == null || "".equals(groupDuty.getGroupName())){
               return BaseResult.body(groupDuty,ResultType.BUSINESS_ERROR).withMessage("角色与组不能同时为空");
           }
        }
        if(groupDuty.getUserTask() == null || "".equals(groupDuty.getUserTask())){
            return BaseResult.body(groupDuty,ResultType.BUSINESS_ERROR).withMessage("任务名称不能为空");
        }
        if(groupDuty.getId() == null || groupDuty.getId() <= 0){
            groupDuty.setId(KeyWorkerUtil.nextId());
            groupDuty.setCreatedAt(new Date());
        }
        groupDuty.setUpdatedAt(new Date());
        groupDuty.setAcitive(true);
        try {
            groupDutyRepository.save(groupDuty);
        }catch (Exception e){
            e.printStackTrace();
            return BaseResult.body(groupDuty,ResultType.BUSINESS_ERROR);
        }
        return BaseResult.body(groupDuty,ResultType.BUSINESS_SUCCESS);
    }


}